﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Redis;
using ServiceStack.Redis.Support;

namespace TestRedis
{
    class Program
    {
        static RedisClient Redis = new RedisClient("127.0.0.1", 6379); //redis服务IP和端口

        static void Main(string[] args)
        {
            Console.WriteLine("----------------------------分割线------------------------");
            TesRunRedis();
            Console.ReadLine();
        }
        public static void TesRunRedis()
        {
            RedisHelper redisHelper = new RedisHelper("127.0.0.1", 6379);
            //移除所以key-value
            RedisHelper.redisClient.GetAllKeys().ForEach(x =>
            {
                RedisHelper.redisClient.Remove(x);
            });
            Console.WriteLine("-------------添加指定类型key-vale-----------------");
            //指定类型key-vale
            redisHelper.TAddValue("mykeystr", "陶伟");
            redisHelper.TAddValue("mykeyint", 12);
            Console.WriteLine($"string：{redisHelper.TGetValeForKey<string>("mykeystr")},int：{redisHelper.TGetValeForKey<string>("mykeyint")}");
            Console.WriteLine("-----------------------添加指定类型集合数据--------------");
            //添加指定类型集合数据
            redisHelper.TAddObjectSerialize("myuserlist",
                new List<UserInfo>()
                {
                    new UserInfo() { UserName = "UserName1" },
                    new UserInfo() { UserName = "UserName2" },
                    new UserInfo() { UserName = "UserName3" }
                });
            redisHelper.TAddObjectSerialize("mystulist",
                new List<Student>()
                {
                    new Student() { StudentName = "StudentName1" },
                    new Student() { StudentName = "StudentName2" },
                    new Student() { StudentName = "StudentName3" }
                });
            redisHelper.TGetSerializeObjForKey<List<UserInfo>>("myuserlist").ToList().ForEach(x =>
            {
                Console.WriteLine(x.UserName);
            });
            Console.WriteLine();
            redisHelper.TGetSerializeObjForKey<List<Student>>("mystulist").ToList().ForEach(x =>
            {
                Console.WriteLine(x.StudentName);
            });
            Console.WriteLine("-----------------------------添加对象数据----------------");
            //添加对象数据
            redisHelper.TAddObject("user", new UserInfo() { UserName = "陶伟" });
            redisHelper.TAddObject("stu", new Student() { StudentName = "张三" });
            Console.WriteLine($"user：{redisHelper.TGetValeForKey<UserInfo>("user").UserName},stu：{redisHelper.TGetValeForKey<UserInfo>("stu").UserName}");
        }
    }
}
